﻿using System;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.Net.Loader;
using Assets.Scripts.UI.Panel;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.UI.Item
{
    public class AnsImageItem:UIBase<AnsImageItem>
    {
        public Image AnsImage;
        public Image RightLable;
        public Image WrongLable;
        public Text AnsText;
        public Toggle Toggle;

        public Action action { get; set; }//定义一个委托
        private AnsInfo ansInfo { get; set; }

        protected override UIType UIType
        {
            get { return UIType.Item;}
        }

#if TEST
        protected override void InitOnDestroy()
        {
            ExamPanel.OnRandomAnswer -= OnRandomAnswer;
        }

        private void OnRandomAnswer()
        {
            System.Random random = new System.Random();
            int randomIndex = random.Next(0, 2);
            Toggle.isOn = randomIndex > 0;
        }
#endif

        protected override void InitOnAwake()
        {
            AutoFindComponent(transform,true);           
        }

        public void Init(AnsInfo info, string label,Action action,ToggleGroup group,bool isCheckScore,QueItem item)
        {
#if TEST
            ExamPanel.OnRandomAnswer += OnRandomAnswer;
#endif
            this.action = action;   
            AnsText.text = label + ":" + info.content;
            ansInfo = info;
            if (group!=null)
            {
                Toggle.group = group;
            }
            //1.加载图片 路径可能还有点问题 
            if (ExamPanel._Instance != null)
            {
                ExamPanel._Instance.StartCoroutine(WWWLoadManager.Instance().LoadLocalResource(info.respath, bytes =>
                {
                    Texture2D texture = new Texture2D((int)AnsImage.rectTransform.rect.width,
                        (int)AnsImage.rectTransform.rect.height);
                    texture.LoadImage(bytes);
                    AnsImage.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
                    item.SetImageContentHeight(AnsImage.GetComponent<RectTransform>().rect.height+70);
                }));
            }
            else
            {
                CollectPanel._Instance.StartCoroutine(WWWLoadManager.Instance().LoadLocalResource(info.respath, bytes =>
                {
                    Texture2D texture = new Texture2D((int)AnsImage.rectTransform.rect.width,
                        (int)AnsImage.rectTransform.rect.height);
                    texture.LoadImage(bytes);
                    AnsImage.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
                    item.SetImageContentHeight(AnsImage.GetComponent<RectTransform>().rect.height + 70);
                }));
            }
            Toggle.interactable = !isCheckScore;
            if (isCheckScore)
            {
                ShowToggleLabel();
                Toggle.isOn = info.ischoose;
                Debug.Log("info.ischoose:" + info.ischoose);
                //将正确答案标记为绿色
                if (info.isright == 1)
                {
                    AnsText.color = new Color(0, 0.5f, 0);
                }
                if (info.ischoose)//如果答案被选择
                {
                    if (info.isright == 1)//答案是正确的
                    {
                        ShowRightLable();
                    }
                    else//答案是错误的
                    {
                        ShowWrongLable();
                        AnsText.color = new Color(0.5f, 0, 0);
                    }
                }
            }
            else
            {
                ShowToggleLabel();
                Toggle.onValueChanged.AddListener(x =>
                {
                    ansInfo.ischoose = x;
                    if (action != null) action();
                });
            }
        }

        private void ShowRightLable()
        {
            RightLable.gameObject.SetActive(true);
            WrongLable.gameObject.SetActive(false);
            Toggle.gameObject.SetActive(false);
        }

        private void ShowWrongLable()
        {
            RightLable.gameObject.SetActive(false);
            WrongLable.gameObject.SetActive(true);
            Toggle.gameObject.SetActive(false);
        }

        private void ShowToggleLabel()
        {
            RightLable.gameObject.SetActive(false);
            WrongLable.gameObject.SetActive(false);
            Toggle.gameObject.SetActive(true);
        }
    }
}